#include "SQLFuse.h"
#include "ui_SQLFuse.h"
#include  <QFileDialog>
#include <algorithm>



SQLFuse::SQLFuse(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SQLFuse)
{
    ui->setupUi(this);
    this->setWindowTitle("数据库融合");
    _newdb = QSqlDatabase::addDatabase("QSQLITE","newDB");
    _olddb = QSqlDatabase::addDatabase("QSQLITE","_olddb");
}

SQLFuse::~SQLFuse()
{
    delete ui;
}

void SQLFuse::on_tb_opendir1_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("打开格点目录"),"db",
                                                      tr("SQL file (*.db)"));
    ui->le_db1->setText(fileName);
    dbTarget = fileName.toStdString();
    SQLHelper::GetDB(dbTarget)->_p_tr->ConnectToDB();
}

void SQLFuse::on_tb_opendir2_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("打开格点目录"),"db",
                                                      tr("SQL file (*.db)"));
    ui->le_db2->setText(fileName);
    dbSource = fileName.toStdString();
    SQLHelper::GetDB(dbSource)->_p_tr->ConnectToDB();
    // SQLHelper::GetDB(dbSource)->p()->prepare("11",1,23,4,5);
}

void SQLFuse::on_pb_start_clicked()
{
    if(dbSource.empty() || dbTarget.empty()){
        showAMessage("没有选择任务单元，无法添加!");
        pushWarn("数据库融合过程中出现问题，当前选择为空!",MU_ERROR_MODULE_SQL);
    }else{
        auto odb = SQLHelper::GetDB(dbTarget)->_p_tr->Get("select CADID,SEAMNAME from SEAMINFO");
        for(int i = 0;i<odb.size() - 1;i += 2){
            DBs[0].push_back(std::make_pair(odb.at(i).toString().toStdString(),odb.at(i+1).toString().toStdString()));
        }
        auto ndb = SQLHelper::GetDB(dbSource)->_p_tr->Get("select CADID,SEAMNAME from SEAMINFO");
        for(int i = 0;i<ndb.size() - 1;i += 2){
            DBs[1].push_back(std::make_pair(ndb.at(i).toString().toStdString(),ndb.at(i+1).toString().toStdString()));
        }
        Separate(); // 分离出差集，并集


    }
}


typedef vector<ReplaceRule>::iterator ruleIter;

void SQLFuse::Separate()
{
    if(DBs[0].at(0) == DBs[1].at(0)){
        std::cout << "==" <<std::endl;
    }else{
        std::cout << "!=" <<std::endl;
    }

//    std::cout <<  DBs[0] .at(0).first <<std::endl;
//    std::cout <<  DBs[0] .at(0).second <<std::endl;

//    std::cout <<  DBs[1] .at(0).first <<std::endl;
//    std::cout <<  DBs[1] .at(0).second <<std::endl;

//    auto iter = unique(DBs[0].begin(),DBs[0].end());
//    if (iter == DBs[0].end())
//    {
//        cout<<"没有重复数据"<<endl;
//    }else{
//        DBs[0].erase(unique(DBs[0].begin(), DBs[0].end()), DBs[0].end());//删除重复值
//    }

    DBs[0].at(5).first = "dsds";
    DBs[0].at(5).second = "13232";

    DBs[0].at(6).first = "13454";
    DBs[0].at(6).second = "545";

//    bool retur = true;
//    while(retur){
//        auto st1 = std::begin(DBs[0]);
//        auto st2 = std::begin(DBs[1]);
//        auto retCode = std::mismatch(st1, std::end(DBs[0]),st2 ,std::end(DBs[1]),
//                [](const ReplaceRule &s1,const ReplaceRule &s2){return s1 == s2; });
//        if (retCode.first == DBs[0].end() && retCode.second == DBs[1].end() )
//        {
//            // cout << "DBs[0] 和 DBs[1]完全相同" << endl;
//            retur = false;
//        }
//        else{
//            st2 = retCode.second;
//            st1 = retCode.first;
//            result[1].push_back(std::make_pair(st2->first,st2->second));
//            st2++;
//            st1++;
//        }

//    }

//    auto retCode = std::mismatch( std::begin(DBs[0]), std::end(DBs[0]),std::begin(DBs[1]) ,std::end(DBs[1]),
//            [](const ReplaceRule &s1,const ReplaceRule &s2){return s1 == s2; });


    //auto retCode = std::mismatch( std::begin(DBs[0]), std::end(DBs[0]),std::begin(DBs[1]) );// ,std::end(DBs[1]));


//    for(auto r = DBs[0].begin();r!= DBs[0].end();r++){
//        for(auto j = DBs[1].begin();j!= DBs[1].end();j++){
//            if((r->first == j->first) && (r->second == j->second)){
//                result[0].push_back(std::make_pair(r->first,r->second));
//            }else{
//                result[1].push_back(std::make_pair(j->first,j->second));
//                result[2].push_back(std::make_pair(r->first,r->second));
//            }
//        }
//    }

//    for(auto r:result[0]){
//        cout<<r.first <<"\t"  <<r.second <<endl;
//    }

//    cout<< "-------------------------------------------------" <<endl;

    for(auto r:result[1]){
        cout<<r.first <<"\t"  <<r.second <<endl;
    }

//    cout<< "-------------------------------------------------" <<endl;
//    for(auto r:result[1]){
//        cout<<r.first <<"\t"  <<r.second <<endl;
//    }



}

void SQLFuse::UpUnion()
{
    auto uni = result[0];   // 获取并集
    for(auto child : uni){
        std::string cadid = child.first;
        std::string seamName = child.second;

        // 并集该版本不处理了
    }
}

void SQLFuse::UpDiff()
{
    auto diff = result[1];   // 获取并集
    for(auto child : diff){
        std::string cadid = child.first;
        std::string seamName = child.second;

        // 并集该版本不处理了
    }
}

void SQLFuse::showAMessage(QString f)
{
    QMessageBox::warning(this, tr("运行失败"),f,QMessageBox::Cancel);
}
